package com.travel.manager.controller;

import com.github.pagehelper.PageInfo;
import com.travel.common.pojo.City;
import com.travel.common.util.JsonUtil;
import com.travel.manager.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/city")
public class CityController {

    @Autowired
    private CityService cityService;

    /**
     * 城市列表页，页面使用easyUI的datagrid
     * @param city 查询条件
     * @param page 页码，参数名不能变
     * @param rows 每页几条，参数名不能变
     * @return
     */
    @RequestMapping("/list.html")
    @ResponseBody
    public Map<String, Object> list(City city, Integer page, Integer rows) {
        PageInfo<City> pageInfo = cityService.getCityList(city, page, rows);
        Map<String, Object> result = new HashMap<>();
        //rows是datagrid需要的数据列表，这个key不能变
        result.put("rows", pageInfo.getList());
        //total是datagrid需要的数据总条数，这个key不能变
        result.put("total", pageInfo.getTotal());
        return result;
    }

    @RequestMapping("/edit.html")
    @ResponseBody
    public Map<String, Object> addCity(City city) {
        if (city.getId() == null) {//如果没有id代表新增
            cityService.addCity(city);
        } else {
            cityService.updateCity(city);
        }
        return JsonUtil.getOkMsg("编辑成功", null);
    }

}
